package com.luoqy.speedy.modular.api.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.luoqy.speedy.core.cache.CacheService;
import com.luoqy.speedy.core.cache.CacheServiceImpl;
import com.luoqy.speedy.data.DataBase;
import com.luoqy.speedy.data.MySqldbUtil;
import com.luoqy.speedy.util.Result;
import com.luoqy.speedy.util.SendMessage;

/**
 * 用户控制器
 * @author luoqy
 * @date 2020年4月5日
 */
@RestController
@RequestMapping("/api/user")
public class H5UserController {
	@RequestMapping("/exit")
	public Result exit(HttpServletRequest req){
			Result result=new Result();
		try {
			HttpSession session=req.getSession(true);
			session.removeAttribute("h5user");
			result.setData(null);
			result.setMessage("退出成功");
			result.setState(1);
			return result;
		} catch (Exception e) {
			// TODO: handle exception
			result.setData(e);
			result.setMessage(e.getMessage());
			result.setState(0);
			return result;
		}
		
	}
	/**
	 * 注册功能
	 * @param req 
	 * @return
	 */
	@RequestMapping("/register")
	public Result register(HttpServletRequest req){
		Result result=new Result();
		String phone=req.getParameter("phone");//需要进行短信验证
		if(null==phone){
			result.setData(null);
			result.setMessage("手机号不能为空");
			result.setState(0);
			return result;
		}
		String password=req.getParameter("password");
		if(null==password){
			result.setData(null);
			result.setMessage("密码不能为空");
			result.setState(0);
			return result;
		}
		Object phoneUser=MySqldbUtil.mysqlSelect("select id from user where phone='"+phone+"'", "none",new DataBase());
		if(null!=phoneUser){
			result.setData(null);
			result.setMessage("用户已存在");
			result.setState(0);
			return result;
		}
		Enumeration<String> parames=req.getParameterNames();
		String set=" set ";
		while(parames.hasMoreElements()){
			String name = (String)parames.nextElement();//调用nextElement方法获得元素
			String value=req.getParameter(name);
			set+=name+"='"+value+"',";
		}
		String time=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
		String sql="insert into user "+set+" createtime='"+time+"',source='h5'";
		int state=MySqldbUtil.mysqlExecute(sql,new DataBase());
		if(state>0){
			String inviteuserid=req.getParameter("inviteuserid");
			if(null!=inviteuserid){
				//邀请好友注册成功获得一本免费书籍观看
				Map<String,Object> inviteuser=(Map<String, Object>) MySqldbUtil.mysqlSelect("select id,gratis from user where id="+inviteuserid, "none",new DataBase());
				int gratis=Integer.valueOf(inviteuser.get("gratis").toString());
				gratis++;
				String sqlStr="update set gratis="+gratis+" user where id="+inviteuserid;
				MySqldbUtil.mysqlExecute(sqlStr,new DataBase());
			}
			result.setData(null);
			result.setMessage("注册成功");
			result.setState(1);
		}else{
			result.setData(null);
			result.setMessage("注册失败");
			result.setState(0);
		}
		return result;
	}
	/**
	 * @param req
	 * @param phone
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	public Result login(HttpServletRequest req,String phone,String password){
		Result result=new Result();
		Object phoneUser=MySqldbUtil.mysqlSelect("select id from user where phone='"+phone+"'", "none",new DataBase());
		if(null==phoneUser){
			result.setData(null);
			result.setMessage("此用户不存在");
			result.setState(0);
			return result;
		}
		Map<String,Object> user=(Map<String,Object>)MySqldbUtil.mysqlSelect("select u.*,um.starttime starttime,um.endtime endtime from user u left join user_member um on u.id=um.userid  where phone='"+phone+"' and password='"+password+"'", "none",new DataBase());
		if(null==user){
			result.setData(null);
			result.setMessage("用户密码错误");
			result.setState(0);
			return result;
		}else{
			String time=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
			MySqldbUtil.mysqlExecute("update user set lasttime='"+time+"' where id="+user.get("id"), new DataBase());
			CacheService cache=new CacheServiceImpl();
			if(cache.isContains("h5user"+phone)){
				cache.clearByKey("h5user"+phone);
			}
			cache.putCache("h5user"+phone, phoneUser, 1000*900L);
			result.setData(user);
			result.setMessage("登录成功");
			result.setState(1);
			return result;
		}
	}
}
